package com.web.manager.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.mapper.FileSubmitMapper;
import com.web.manager.pojo.FileSubmit;
import com.web.manager.pojo.Result;
import com.web.manager.pojo.User;
import com.web.manager.service.FileSubmitService;
import com.web.manager.pojo.*;
import com.web.manager.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/fileSubmit")
public class FileSubmitController {
    @Autowired
    private FileSubmitService fileSubmitService;

    @Resource
    private FileSubmitMapper fileSubmitMapper;

    @GetMapping("/submit/{publisher}")
    public Result getById(@PathVariable String publisher){

        List<FileSubmit> deptList =fileSubmitMapper.getByPublisher(publisher);
        return Result.success(deptList);
    }

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody FileSubmit type){
        User currentUser = TokenUtils.getCurrentUser();
        type.setPublisher(currentUser.getUsername());
        type.setTime(DateUtil.now());
        fileSubmitService.save(type);
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody FileSubmit type){
        fileSubmitService.updateById(type);  //updateUser
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        fileSubmitService.removeById(id);  //deleteUser
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        fileSubmitService.removeBatchByIds(ids);   //batchDeleteUser
        return Result.success();
    }
    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String name) {

        QueryWrapper<FileSubmit> queryWrapper = new QueryWrapper<>();  //条件构造器
        queryWrapper.like(StrUtil.isNotBlank(name),"name",name);
        //select * from manager_user where username like '%#{username}% and name like '%#{name}%'

        Page<FileSubmit> page = fileSubmitService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
        return Result.success(page);
    }

}
